package com.zhangxi.websqlit.file.control;

import com.alibaba.fastjson.JSON;
import com.zhangxi.websqlit.common.control.BaseController;
import com.zhangxi.websqlit.file.entity.SearchFileInfoEntity;
import com.zhangxi.websqlit.file.service.FileInfoService;
import com.zhangxi.websqlit.mybatis.entity.PtFileEntity;
import com.zhangxi.websqlit.mybatis.flexigrid.PadingRstType;
import com.zhangxi.websqlit.mybatis.flexigrid.PagingBean;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/file")
public class FileInfoController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());
    @Resource
    private FileInfoService fileInfoService;

    @RequestMapping("filePage")
    public String initPage(){
        return "file/filePage";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public String uploadFile(MultipartHttpServletRequest request) throws IOException {
        MultipartFile file = request.getFile("fileName");
        String fileName = file.getOriginalFilename();
        String parentUuid = request.getParameter("parentUuid");
        logger.info("fileName:" + fileName + " parentUuid:" + parentUuid);
        fileInfoService.uploadFile(fileName,file.getInputStream(),parentUuid);
        return jsonSuccess("file.upload.success");
    }
    @RequestMapping("/getFileListByPage")
    @ResponseBody
    public PadingRstType<PtFileEntity> getFileListByPage(SearchFileInfoEntity entity, PagingBean paging){
        logger.info(paging);
        PadingRstType<PtFileEntity> padingRstType = fileInfoService.getFileListByPage(entity,paging);
        return padingRstType;
    }

    @RequestMapping("/mkdir")
    @ResponseBody
    public String mkdir(PtFileEntity entity){
        logger.info(entity);
        fileInfoService.mkdir(entity);
        return jsonSuccess("file.mkdir.success");
    }
    @RequestMapping("/queryFileInfoById")
    @ResponseBody
    public String queryFileInfoById(PtFileEntity entity){
        logger.info(entity);
        String filePath = fileInfoService.queryFileInfoById(entity.getFileUuid());
        Map<String,Object> map = new HashMap<>();
        map.put("success",true);
        map.put("msg",filePath);
        map.put("description",filePath);
        return JSON.toJSONString(map);
    }
    @RequestMapping("/queryFileParentById")
    @ResponseBody
    public PtFileEntity queryFileParentById(PtFileEntity entity){
        logger.info(entity);
        PtFileEntity ptFileEntity = fileInfoService.queryFileParentById(entity.getFileUuid());
        return ptFileEntity;
    }

    @RequestMapping("/download/{fileUUid}")
    @ResponseBody
    public ResponseEntity<byte[]> download(@PathVariable("fileUUid") String fileUUId) throws IOException {
        logger.info("fileUUid:" + fileUUId);

        ResponseEntity<byte[]> responseEntity = fileInfoService.download(fileUUId);
        return responseEntity;
    }

    @RequestMapping("/queryFileList")
    @ResponseBody
    public List<PtFileEntity> queryFileList(){
        List<PtFileEntity> list = fileInfoService.queryFileList();
        return list;
    }

    /**
     * 手机下载文式
     * @param fileUUId
     * @param fileName
     * @return
     * @throws IOException
     */
    @RequestMapping("/downloadPhone/{fileUUid}/{fileName}")
    @ResponseBody
    public ResponseEntity<byte[]> downloadPhone(@PathVariable("fileUUid") String fileUUId,
                                                @PathVariable("fileName") String fileName) throws IOException {
        logger.info("fileUUid:" + fileUUId + "fileName:" + fileName);

        ResponseEntity<byte[]> responseEntity = fileInfoService.downloadPhone(fileUUId);
        return responseEntity;
    }




}
